﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GeoSyndication.Common;

namespace GeoSyndication.KML
{
    public class KMLData
    {
        #region Private Fields

        string title;
        string description;
        List<KMLItem> items;

        #endregion

        #region Public Properties

        public string Title
        {
            get { return title; }
        }

        public string Description
        {
            get { return description; }
        }

        public List<KMLItem> Items
        {
            get { return items; }
        }

        #endregion

        #region Constructor

        public KMLData(string title, string description)
        {
            this.description = description;
            this.title = title;
            items = new List<KMLItem>();
        } 
        
        #endregion

        #region Public Methods

        public KMLItem Add(string title, string content, string link, DateTime date, string categories)
        {
            KMLItem a = new KMLItem() { Title = title, Description = content, Link = link, Categories = Utility.ParseCategories(categories), Date = date };
            a.Categories = Utility.ParseCategories(categories);
            items.Add(a);
            return a;
        }

        public KMLPointItem AddPoint(string title, string content, string link, DateTime date, string categories, double latitude, double longitude)
        {
            KMLPointItem p = new KMLPointItem() { Title = title, Link = link, Description = content, Categories = Utility.ParseCategories(categories), Date = date };
            p.Point.Format = "{lng} {lat} {alt}";
            p.Point = new Point(latitude, longitude);
            p.Categories = Utility.ParseCategories(categories);
            items.Add(p);
            return p;
        }

        public KMLPolygonItem AddPolygon(string title, string content, string link, DateTime date, string categories, string polygon)
        {
            return AddPolygon(title, description, link, date, categories, Point.ParsePoints(polygon));
        }

        public KMLPolygonItem AddPolygon(string title, string content, string link, DateTime date, string categories, Point[] polygon)
        {
            if (polygon.Length < 3)
            {
                throw new ArgumentException("A polygon should contain at least 3 points");
            }

            if (polygon[0].Latitude != polygon[polygon.Length - 1].Latitude || polygon[0].Longitude != polygon[polygon.Length - 1].Longitude)
            {
                throw new ArgumentException("The last and first points in a polygon should be the same");
            }

            KMLPolygonItem p = new KMLPolygonItem() { Title = title, Description = content, Link = link, Categories = Utility.ParseCategories(categories), Date = date };

            p.Polygon = polygon;
            p.Categories = Utility.ParseCategories(categories);
            items.Add(p);
            return p;
        }

        public KMLLineItem AddLine(string title, string content, string link, DateTime date, string categories, string line)
        {
            return AddLine(title, content, link, date, categories, Point.ParsePoints(line));
        }

        public KMLLineItem AddLine(string title, string content, string link, DateTime date, string categories, Point[] line)
        {
            if (line.Length < 2)
            {
                throw new ArgumentException("A line should contain at least 2 points");
            }
            KMLLineItem p = new KMLLineItem() { Title = title, Description = content, Link = link, Categories = Utility.ParseCategories(categories), Date = date };

            p.Line = line;
            p.Categories = Utility.ParseCategories(categories);
            items.Add(p);
            return p;
        }

        #endregion
    }
}
